<?php
namespace App\Http\Controllers\Admin;

use App\Models\Comments;
use App\Models\Teaches;
use App\Models\TeachesCompany;
use App\Models\TeachesContent;
use App\Models\EmployComments;
use App\Models\EmployExt;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class CommentsController extends BaseController
{
    
    /**
     * 就业指南 评论列表
     */
    public function employList() 
    {
        if(!$this->user->can('viewEmployComment')) abort(405);
        $result = EmployComments::where(array('to_comment_id'=>0))->paginate(20);
        return view('admin.cmscomment.employ')->with('result',$result);
    }

    /**
     * 就业指南评论删除
     */
    public function destroye(Request $request)
    {
        $id = $request->input('comment_id');
        $comment = EmployComments::where(array('comment_id'=>$id,'to_comment_id'=>0))->first();
        if(empty($comment))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::beginTransaction();
            try{
                //删除子评论,评论数减一
                EmployComments::where('to_comment_id', '=', $comment->comment_id)->delete();
                $exts = EmployExt::findOrNew($comment->employ_id);
                $exts->decrementDel('comment_num',1);
                $comment->delete();
                DB::commit();
            }catch (\PDOException $e){
                DB::rollback();
                return $this->formatJson('falied','评论删除失败');
            }
            return $this->formatJson('success','评论删除成功！');
        }
    }
    
    /**
     * 充电加油 评论列表
     */
    public function teachesList()
    {
        if(!$this->user->can('viewTeachesComment')) abort(405);
        $result = Comments::whereIn('app_table', array('teaches', 'tcompany','tcourse'))->where(array('to_comment_id'=>0))->paginate(20);
        return view('admin.cmscomment.teaches')->with('result',$result);
    }

    /**
     * 充电加油评论删除
     */
    public function destroyt(Request $request)
    {
        $id = $request->input('comment_id');
        $comment = Comments::where(array('comment_id'=>$id,'to_comment_id'=>0))->first();
        if(empty($comment))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            if($comment->app_table == 'teaches'){ //职场评论
                DB::beginTransaction();
                try{
                    //删除子评论,评论数减一
                    Comments::where('to_comment_id', '=', $comment->comment_id)->delete();
                    $teac = Teaches::findOrNew($comment->app_id);
                    $teac->decrementDel('comments_num',1);
                    $comment->delete();
                    DB::commit();
                }catch (\PDOException $e){
                    DB::rollback();
                    return $this->formatJson('falied','评论删除失败');
                }
                return $this->formatJson('success','评论删除成功！');
            }else if($comment->app_table == 'tcompany'){ //内训评论
                DB::beginTransaction();
                try{
                    //删除子评论,评论数减一
                    Comments::where('to_comment_id', '=', $comment->comment_id)->delete();
                    $teac = TeachesCompany::findOrNew($comment->app_id);
                    $teac->decrementDel('comments_num',1);
                    $comment->delete();
                    DB::commit();
                }catch (\PDOException $e){
                    DB::rollback();
                    return $this->formatJson('falied','评论删除失败');
                }
                return $this->formatJson('success','评论删除成功！');
            }else if($comment->app_table == 'tcourse'){  //教程评论

                DB::beginTransaction();
                try{
                    //删除子评论,评论数减一
                    Comments::where('to_comment_id', '=', $comment->comment_id)->delete();
                    $teac = TeachesContent::findOrNew($comment->app_id);
                    $teac->decrementDel('comments_num',1);
                    $comment->delete();
                    DB::commit();
                }catch (\PDOException $e){
                    DB::rollback();
                    return $this->formatJson('falied','评论删除失败');
                }
                return $this->formatJson('success','评论删除成功！');
            }
        }
    }
}
